<?php


namespace app\v1_0\controller\company;


use app\common\model\CompanyStructure;

class RecruitTeam extends \app\v1_0\controller\common\Base
{
    public function _initialize()
    {
        parent::_initialize();
        $this->checkLogin(1);
        $this->interceptCompanyProfile();
    }

    public function index(){
        $current_page = input('get.page/d', 1, 'intval');
        $pagesize = input('get.pagesize/d', 10, 'intval');

        $where = ['com_id'=>$this->company_profile['id'],'recruit_team'=>'是'];
        $list = model('CompanyStaffManage')
            ->alias('cs')
            ->field('cs.id,name,mobile,mailbox,coms_d_id')
            ->where($where)
//            ->join(config('database.prefix').'company c','c.id=cs.com_id')
            ->page($current_page, $pagesize)
            ->select();
        foreach ($list as $value){
            $coms_d_id = explode(',',$value['coms_d_id']);
            $department_id = end($coms_d_id);
            $department_ids[] = $department_id;
            $value['department_id'] = $department_id;
        }
        $departments = CompanyStructure::whereIn('id',$department_ids??[])->column('name','id');
        foreach ($list as $key=>$val){
            $dep_name = $departments[$val['department_id']]??'';
            $val['departmentname'] = $dep_name;
        }
        $return['items'] = $list;
        $return['total'] = model('CompanyStaffManage')->where($where)->count();
        $this->ajaxReturn(200, '获取数据成功', $return);
    }

    //获取招聘人员
    public function getrecruitList()
    {
        $list = model('CompanyStaffManage')
            ->alias('cs')
            ->field('cs.id,name,mobile,mailbox,c.companyname,coms_d_id')
            ->where(['com_id'=>$this->company_profile['id'],'recruit_team'=>'是'])
            ->join(config('database.prefix').'company c','c.id=cs.com_id')
            ->select();
        $ret = [];
        foreach ($list as $value){
            $coms_d_id = explode(',',$value['coms_d_id']);
            $department_id = end($coms_d_id);
            $department_ids[] = $department_id;
            $value['department_id'] = $department_id;
        }
        $departments = CompanyStructure::whereIn('id',$department_ids??[])->column('name','id');
        if(!empty($list)){
            foreach ($list as $key=>$val){
                $dep_name = $departments[$val['department_id']]??'';
                $ret[] = ['label'=>($dep_name?$dep_name.'-':'').$val['name'],'value'=>$val['id']];
            }
        }
        $this->ajaxReturn(200, '获取数据成功', $ret);
    }



}